package com.weavernorth.statement.service;

import com.alibaba.fastjson.JSONObject;
import com.weavernorth.statement.pojo.dataVo;
import com.weavernorth.statement.pojo.detailOneVo;
import com.weavernorth.statement.pojo.detailTwoVo;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.MyCommonUtils;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.workflow.webservices.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.statement.service
 * @date: 2021/6/18 15:38
 * @description: 获取SAP商场对账单数据并自动创建流程
 */
public class mallStateMentService {
//    private Logger log = Logger4jUtil.getLoggerByName("/action/api/statementApi/");

    public JSONObject createWorkFlow(Logger log,JSONObject jsonData) {
        BaseBean bb = new BaseBean();
        JSONObject jsonResult = new JSONObject();
        log.info("商场对账单流程自动创建接口-开始：");
        String table_Main = bb.getPropValue("mallProps", "mainTable");
        //是否自动流转到下一节点审批人,1:是；0：否
        String isNextNode = bb.getPropValue("mallProps", "isNextNode");
        Boolean isExists_dzdh = false;
        try {
            dataVo datavo = JSONObject.toJavaObject(jsonData, dataVo.class);
            log.info("json转对象：" + datavo.toString());
            //申请人
            String sqr = datavo.getSqr();
            //根据工号获取人员id
            String strUserid = MyCommonUtils.getUseridByCode(sqr);
            //商场对账单号
            String dzdh = datavo.getDzdh();
            //门店编码
            String mdbm = datavo.getMdbm();
            //根据门店编码获取店经理，如果不存在，则传入申请人
            String djl = getIdByCode(mdbm, strUserid);
            log.info("获取店经理：" + djl);
            //商城编码
            String scbm = datavo.getScbm();
            //结算月份
            String jsyf = datavo.getJsyf();
            //备注
            String bz = datavo.getBz();
            List<detailOneVo> detailOneList = datavo.getDetailOneList();//明细表1
            List<detailTwoVo> detailTwoList = datavo.getDetailTwoList();//明细表2

            //region [创建流程的主表]
            WorkflowRequestTableField[] wrMain = new WorkflowRequestTableField[9];

            //申请人
            wrMain[0] = new WorkflowRequestTableField();
            wrMain[0].setFieldName("sqr");
            wrMain[0].setFieldValue(strUserid);
            wrMain[0].setView(true);
            wrMain[0].setEdit(true);
            wrMain[0].setFieldOrder(0);
            wrMain[0].setMand(false);
            //申请时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String strCreateDate = df.format(new Date());
            wrMain[1] = new WorkflowRequestTableField();
            wrMain[1].setFieldName("sqsj");
            wrMain[1].setFieldValue(strCreateDate);
            wrMain[1].setView(true);
            wrMain[1].setEdit(true);
            wrMain[1].setFieldOrder(0);
            wrMain[1].setMand(false);

            //填报部门
            String strDep = MyCommonUtils.getDepByCode(sqr);
            log.info("填报部门：");
            wrMain[2] = new WorkflowRequestTableField();
            wrMain[2].setFieldName("bm");
            wrMain[2].setFieldValue(strDep);
            wrMain[2].setView(true);
            wrMain[2].setEdit(true);
            wrMain[2].setFieldOrder(0);
            wrMain[2].setMand(false);
            //对账单号
            wrMain[3] = new WorkflowRequestTableField();
            wrMain[3].setFieldName("dzdh");
            wrMain[3].setFieldValue(dzdh);
            wrMain[3].setView(true);
            wrMain[3].setEdit(true);
            wrMain[3].setFieldOrder(0);
            wrMain[3].setMand(false);

            //门店编码
            wrMain[4] = new WorkflowRequestTableField();
            wrMain[4].setFieldName("mdbm");
            wrMain[4].setFieldValue(mdbm);
            wrMain[4].setView(true);
            wrMain[4].setEdit(true);
            wrMain[4].setFieldOrder(0);
            wrMain[4].setMand(false);

            //商城编码
            wrMain[5] = new WorkflowRequestTableField();
            wrMain[5].setFieldName("scbm");
            wrMain[5].setFieldValue(scbm);
            wrMain[5].setView(true);
            wrMain[5].setEdit(true);
            wrMain[5].setFieldOrder(0);
            wrMain[5].setMand(false);

            //结算月份
            wrMain[6] = new WorkflowRequestTableField();
            wrMain[6].setFieldName("jsyf");
            wrMain[6].setFieldValue(jsyf);
            wrMain[6].setView(true);
            wrMain[6].setEdit(true);
            wrMain[6].setFieldOrder(0);
            wrMain[6].setMand(false);

            //商城编码
            wrMain[7] = new WorkflowRequestTableField();
            wrMain[7].setFieldName("dc");
            wrMain[7].setFieldValue(djl);
            wrMain[7].setView(true);
            wrMain[7].setEdit(true);
            wrMain[7].setFieldOrder(0);
            wrMain[7].setMand(false);

            //备注字段
            wrMain[8] = new WorkflowRequestTableField();
            wrMain[8].setFieldName("bz");
            wrMain[8].setFieldValue(bz);
            wrMain[8].setView(true);
            wrMain[8].setEdit(true);
            wrMain[8].setFieldOrder(0);
            wrMain[8].setMand(false);

            String workflowid = bb.getPropValue("mallProps", "flowid");
            log.info("workflowid的值：" + workflowid);
            //配置流程基本信息:workflowId
            WorkflowBaseInfo wbi = new WorkflowBaseInfo();
            wbi.setWorkflowId(workflowid);
            //填装主表相关字段对象
            WorkflowRequestTableRecord[] wrtri_m = new WorkflowRequestTableRecord[1];
            wrtri_m[0] = new WorkflowRequestTableRecord();
            wrtri_m[0].setWorkflowRequestTableFields(wrMain);
            //创建流程主表信息对象
            WorkflowMainTableInfo wmi_m = new WorkflowMainTableInfo();
            wmi_m.setRequestRecords(wrtri_m);

            WorkflowRequestInfo wrInfo = new WorkflowRequestInfo();
            wrInfo.setRequestLevel("0");//紧急程度
            wrInfo.setRequestName("【品牌】商场对账单审批流程");//流程的名称
            //0:停留在提交节点;1:自动流转到下一节点
            wrInfo.setIsnextflow(isNextNode);
            //主表数据
            wrInfo.setWorkflowMainTableInfo(wmi_m);
            wrInfo.setWorkflowBaseInfo(wbi);
            //endregion

            //region [填装明细表]
            WorkflowDetailTableInfo[] wdti = new WorkflowDetailTableInfo[2];
            //region [明细表1]
            if (detailOneList != null) {
                wrtri_m = getDetail_1(log,detailOneList);
                wdti[0] = new WorkflowDetailTableInfo();
                wdti[0].setWorkflowRequestTableRecords(wrtri_m);
            }
            //endregion
            //region [明细表2]
            if (detailTwoList != null) {
                wrtri_m = getDetail_2(log,detailTwoList);
                wdti[1] = new WorkflowDetailTableInfo();
                wdti[1].setWorkflowRequestTableRecords(wrtri_m);
            }
            //endregion

            wrInfo.setWorkflowDetailTableInfos(wdti);
            wrInfo.setCreatorId(strUserid);//流程创建人
            //endregion

            WorkflowServiceImpl wfsi = new WorkflowServiceImpl();
            String requestid = wfsi.doCreateWorkflowRequest(wrInfo, Integer.parseInt(strUserid));
            log.info("【品牌】商场对账单返回值==" + requestid);
            if (!"".equals(requestid) && Integer.parseInt(requestid) > 0) {
                log.info("【品牌】商场对账单流程创建成功！");
                jsonResult.put("status", "S");
                jsonResult.put("message", "流程创建成功！");
            } else {
                jsonResult.put("status", "F");
                jsonResult.put("message", "【品牌】商场对账单审批流程创建失败,失败原因：找不到下一节点审批人！");
                log.info("【品牌】商场对账单审批流程创建失败,失败原因：找不到下一节点审批人！");
            }
        } catch (Exception ex) {
            log.info("解析SAP接口数据发生异常,异常信息：" + ex.toString());
            jsonResult.put("status", "F");
            jsonResult.put("message", "解析数据失败,异常信息:" + ex.toString());
        }
        return jsonResult;
    }

    /***
     * 根据店铺sapid获取店经理id
     * @param mdbm
     * @return
     */
    private String getIdByCode(String mdbm, String sqr) {
        String strResult = sqr;
        String strQuery = "select dc from uf_dpxx where dpsapid='" + mdbm + "'";
        RecordSet rs = new RecordSet();
        rs.execute(strQuery);
        while (rs.next()) {
            strResult = Util.null2String(rs.getString("dc"));
        }
        return strResult;
    }

    /***
     * 明细表2
     * @param detailTwoList
     * @return
     */
    private WorkflowRequestTableRecord[] getDetail_2(Logger log,List<detailTwoVo> detailTwoList) {
        log.info("明细表2：" + detailTwoList.toString());
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[detailTwoList.size()];
        WorkflowRequestTableField[] wrti;
        int i = 0;
        for (detailTwoVo d2 : detailTwoList) {
            wrti = new WorkflowRequestTableField[5];
            //项目费用
            wrti[0] = new WorkflowRequestTableField();
            wrti[0].setFieldName("fyxm");
            wrti[0].setFieldValue(d2.getFyxm());
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            //预估费用金额
            wrti[1] = new WorkflowRequestTableField();
            wrti[1].setFieldName("htfyje");
            wrti[1].setFieldValue(d2.getHtfyje());
            wrti[1].setView(true);
            wrti[1].setEdit(true);
            //商场费用金额
            wrti[2] = new WorkflowRequestTableField();
            wrti[2].setFieldName("jsdje");
            wrti[2].setFieldValue(d2.getJsdje());
            wrti[2].setView(true);
            wrti[2].setEdit(true);
            //系统实销金额
            wrti[3] = new WorkflowRequestTableField();
            wrti[3].setFieldName("cyje");
            wrti[3].setFieldValue(d2.getCyje());
            wrti[3].setView(true);
            wrti[3].setEdit(true);

            //备注
            wrti[4] = new WorkflowRequestTableField();
            wrti[4].setFieldName("bz");
            wrti[4].setFieldValue(d2.getBz());
            wrti[4].setView(true);
            wrti[4].setEdit(true);

            wrtri[i] = new WorkflowRequestTableRecord();
            wrtri[i].setWorkflowRequestTableFields(wrti);
            i++;
        }
        return wrtri;
    }

    /***
     * 明细表1
     * @param detailOneList
     * @return
     */
    private WorkflowRequestTableRecord[] getDetail_1(Logger log,List<detailOneVo> detailOneList) {
        log.info("明细表1：" + detailOneList.toString());
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[detailOneList.size()];
        WorkflowRequestTableField[] wrti;
        int i = 0;
        for (detailOneVo d1 : detailOneList) {
            wrti = new WorkflowRequestTableField[9];
            //系统销售数量
            wrti[0] = new WorkflowRequestTableField();
            wrti[0].setFieldName("xtxssl");
            wrti[0].setFieldValue(d1.getXtxssl());
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            //结算单销售数量
            wrti[1] = new WorkflowRequestTableField();
            wrti[1].setFieldName("jsdxssl");
            wrti[1].setFieldValue(d1.getJsdxssl());
            wrti[1].setView(true);
            wrti[1].setEdit(true);
            //数量差异
            wrti[2] = new WorkflowRequestTableField();
            wrti[2].setFieldName("slcy");
            wrti[2].setFieldValue(d1.getSlcy());
            wrti[2].setView(true);
            wrti[2].setEdit(true);
            //系统实销金额
            wrti[3] = new WorkflowRequestTableField();
            wrti[3].setFieldName("xtsxje");
            wrti[3].setFieldValue(d1.getXtsxje());
            wrti[3].setView(true);
            wrti[3].setEdit(true);

            //结算单实收金额
            wrti[4] = new WorkflowRequestTableField();
            wrti[4].setFieldName("jsdsxje");
            wrti[4].setFieldValue(d1.getJsdsxje());
            wrti[4].setView(true);
            wrti[4].setEdit(true);

            //实收差异
            wrti[5] = new WorkflowRequestTableField();
            wrti[5].setFieldName("jecy");
            wrti[5].setFieldValue(d1.getJecy());
            wrti[5].setView(true);
            wrti[5].setEdit(true);

            //系统结算金额
            wrti[6] = new WorkflowRequestTableField();
            wrti[6].setFieldName("xtjsje");
            wrti[6].setFieldValue(d1.getXtjsje());
            wrti[6].setView(true);
            wrti[6].setEdit(true);

            //结算单结算金额
            wrti[7] = new WorkflowRequestTableField();
            wrti[7].setFieldName("jsdjsje");
            wrti[7].setFieldValue(d1.getJsdjsje());
            wrti[7].setView(true);
            wrti[7].setEdit(true);

            //结算差异
            wrti[8] = new WorkflowRequestTableField();
            wrti[8].setFieldName("jscy");
            wrti[8].setFieldValue(d1.getJscy());
            wrti[8].setView(true);
            wrti[8].setEdit(true);
            wrtri[i] = new WorkflowRequestTableRecord();
            wrtri[i].setWorkflowRequestTableFields(wrti);
            i++;
        }
        return wrtri;
    }

}
